﻿using System;
using AutoMapper;
using PHMEE.CommonUtils;
using PHMEE.DTOS.Diagnosis;
using PHMEE.DTOS.StatusMonitor;
using PHMEE.EnergyEfficiency;
using PHMEE.Entities.Diagnosis;
using Volo.Abp.AuditLogging;

namespace PHMEE;

public class PHMEEApplicationAutoMapperProfile : Profile
{
    public PHMEEApplicationAutoMapperProfile()
    {

        CreateMap<AuditLog, PagingAuditLogOutput>()
            .ForMember(dest => dest.ExecutionTime,
                opt => opt.MapFrom(s => s.ExecutionTime.ToString("O")));
        CreateMap<AuditLogAction, PagingAuditLogActionOutput>()
            .ForMember(dest => dest.ExecutionTime,
                opt => opt.MapFrom(s => s.ExecutionTime.ToString("O")));
        CreateMap<EntityChange, PagingEntityChangeOutput>()
            .ForMember(dest => dest.ChangeTypeDescription,
                opt => opt.MapFrom(s => s.ChangeType))
            .ForMember(dest => dest.ChangeTime,
                opt => opt.MapFrom(s => s.ChangeTime.ToString("O")));
        CreateMap<EntityPropertyChange, PagingEntityPropertyChangeOutput>();


        CreateMap<MassFlowmeter, MassFlowmeterExcelDto>()
            // 当 MassFlowmeter.Timestamp 不为 null 时，转为 UniversalTime 后再 ToString
            .ForMember(dest => dest.Timestamp, opt =>
                opt.MapFrom(src => src.Timestamp == null
                    ? null
                    : src.Timestamp.Value
                           .ToUniversalTime()
                           .ToString("yyyy-MM-dd HH:mm:ss.fff")));

#nullable enable

        #region Diagnosis

        CreateMap<AlarmRecord, AlarmRecordAckStatusDto>();

        #endregion Diagnosis

        #region EnergyEfficiency

        CreateMap<LogAbstractDto, LogAbstract>()
            .ForMember(dest => dest.IMO, opt => opt.MapFrom(src => src.IMO))
            .ForMember(dest => dest.DateUTC, opt => opt.MapFrom(src => src.DateUTC.ToString("yyyy-MM-dd")))
            .ForMember(dest => dest.TimeUTC,
                opt => opt.MapFrom(
                    src => DateTimeUtils.GetTimeOnlyUpToMinute(src.TimeUTC).ToString("HH:mm:ss")))
            .ReverseMap()
            .ForPath(dest => dest.DateUTC, opt => opt.MapFrom(src => DateOnly.Parse(src.DateUTC)))
            .ForPath(dest => dest.TimeUTC, opt => opt.MapFrom(src => TimeOnly.Parse(src.TimeUTC)));

        CreateMap<MassFlowmeter, MassFlowmeterDto>();
        CreateMap<DeviceFuelConsumption, DeviceFuelConsumptionDto>();
        #endregion LogAbstract

#nullable disable
    }
}
